//
//  PieChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "PieChartViewController.h"
@interface PieChartViewController ()

@end

@implementation PieChartViewController
PieChartView *samplePieChartView;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initPieChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initPieChartView{
    samplePieChartView=[[PieChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    samplePieChartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:samplePieChartView];
    [self initPieChartStyleAndData];
}

-(void)initPieChartStyleAndData{
    [self setStyle];
    [self setHollowStyle];
    [self setDrawHoleText];
    [self setLegendStyle];
    [self setDescription];
    [samplePieChartView setData:[self setData]];
}

// 基本样式
-(void)setStyle{
    //饼状图距离边缘的间隙
    [samplePieChartView setExtraOffsetsWithLeft:30 top:0 right:30 bottom:0];
    //是否根据所提供的数据, 将显示数据转换为百分比格式
    samplePieChartView.usePercentValuesEnabled = YES;
    //拖拽饼状图后是否有惯性效果
    samplePieChartView.dragDecelerationEnabled = YES;
    //是否显示区块文本
    samplePieChartView.drawSliceTextEnabled = YES;
}

//饼状图中间的空心样式
-(void)setHollowStyle{
    //饼状图是否是空心
    samplePieChartView.drawHoleEnabled = YES;
    //空心半径占比
    samplePieChartView.holeRadiusPercent = 0.5;
    //空心颜色
    samplePieChartView.holeColor = [UIColor clearColor];
    //半透明空心半径占比
    samplePieChartView.transparentCircleRadiusPercent = 0.52;
    //半透明空心的颜色
    samplePieChartView.transparentCircleColor = [UIColor colorWithRed:210/255.0 green:145/255.0 blue:165/255.0 alpha:0.3];
}

//设置饼状图中心的文本
-(void)setDrawHoleText{
    if (samplePieChartView.isDrawHoleEnabled == YES) {
        samplePieChartView.drawCenterTextEnabled = YES;//是否显示中间文字
        //普通文本
        //        samplePieChartView.centerText = @"饼状图";//中间文字
        //富文本
        NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"饼状图"];
        [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
                                    NSForegroundColorAttributeName: [UIColor orangeColor]}
                            range:NSMakeRange(0, centerText.length)];
        samplePieChartView.centerAttributedText = centerText;
    }
}

//设置饼状图描述
-(void)setDescription{
    samplePieChartView.descriptionText = @"饼状图示例";
    samplePieChartView.descriptionFont = [UIFont systemFontOfSize:10];
    samplePieChartView.descriptionTextColor = [UIColor grayColor];
}

//设置饼状图图例样式
-(void)setLegendStyle{
    //图例在饼状图中的大小占比, 这会影响图例的宽高
    samplePieChartView.legend.maxSizePercent = 1;
    //文本间隔
    samplePieChartView.legend.formToTextSpace = 5;
    //字体大小
    samplePieChartView.legend.font = [UIFont systemFontOfSize:10];
    //字体颜色
    samplePieChartView.legend.textColor = [UIColor grayColor];
    //图例在饼状图中的位置
    samplePieChartView.legend.position = ChartLegendPositionBelowChartCenter;
    //图示样式: 方形、线条、圆形
    samplePieChartView.legend.form = ChartLegendFormCircle;
    //图示大小
    samplePieChartView.legend.formSize = 12;
}

- (PieChartData *)setData{
    
    double mult = 100;
    int count = 5;//饼状图总共有几块组成
    
    //每个区块的数据
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++) {
        double randomVal = arc4random_uniform(mult + 1);
        BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithValue:randomVal xIndex:i];
        [yVals addObject:entry];
    }
    
    //每个区块的名称或描述
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++) {
        NSString *title = [NSString stringWithFormat:@"part%d", i+1];
        [xVals addObject:title];
    }
    
    //dataSet
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithYVals:yVals label:@""];
    dataSet.drawValuesEnabled = YES;//是否绘制显示数据
    NSMutableArray *colors = [[NSMutableArray alloc] init];
    [colors addObjectsFromArray:ChartColorTemplates.vordiplom];
    [colors addObjectsFromArray:ChartColorTemplates.joyful];
    [colors addObjectsFromArray:ChartColorTemplates.colorful];
    [colors addObjectsFromArray:ChartColorTemplates.liberty];
    [colors addObjectsFromArray:ChartColorTemplates.pastel];
    [colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
    dataSet.colors = colors;//区块颜色
    dataSet.sliceSpace = 0;//相邻区块之间的间距
    dataSet.selectionShift = 8;//选中区块时, 放大的半径
    dataSet.xValuePosition = PieChartValuePositionInsideSlice;//名称位置
    dataSet.yValuePosition = PieChartValuePositionOutsideSlice;//数据位置
    //数据与区块之间的用于指示的折线样式
    dataSet.valueLinePart1OffsetPercentage = 0.85;//折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远
    dataSet.valueLinePart1Length = 0.5;//折线中第一段长度占比
    dataSet.valueLinePart2Length = 0.4;//折线中第二段长度最大占比
    dataSet.valueLineWidth = 1;//折线的粗细
    dataSet.valueLineColor = [UIColor brownColor];//折线颜色
    
    //data
    PieChartData *data = [[PieChartData alloc] initWithXVals:xVals dataSet:dataSet];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterPercentStyle;
    formatter.maximumFractionDigits = 0;//小数位数
    formatter.multiplier = @1.f;
    [data setValueFormatter:formatter];//设置显示数据格式
    [data setValueTextColor:[UIColor brownColor]];
    [data setValueFont:[UIFont systemFontOfSize:10]];
    
    return data;
}


@end